{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd3d9572",
   "metadata": {},
   "source": [
    "# Python 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7083c9d",
   "metadata": {},
   "source": [
    "一些关于Python的小知识：\n",
    "- Python的作者是荷兰人吉多·范罗苏姆（Guido van Rossum）。\n",
    "- 第一版Python的诞生时间是1989年圣诞节假期。\n",
    "- Python是一种蟒蛇的名字，所以Python的标志上有蛇的样式。\n",
    "- Python名字的由来，据说是因为作者本人当时是BBC电视剧蒙提·派森的飞行马戏团（Monty Python's Flying Circus）的粉丝。\n",
    "- Python的设计哲学是优雅、明确、简单。\n",
    "- Python 3于2008年12月3日发布，不完全兼容Python 2。2020年1月1日起，Python不再提供对Python 2版本的官方支持。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abfd1aec",
   "metadata": {},
   "source": [
    "## Python 3环境的安装\n",
    "\n",
    "初学者可以下载Anaconda做为Python的安装环境，特别是在Windows系统中，下载地址为：\n",
    "\n",
    "https://www.anaconda.com/\n",
    "\n",
    "Mac/Linux系统下，除了Anaconda，稍微有基础的情况下，也可以自己安装Python 3环境。有一些系统发行版自带Python，不过需要看下是不是Python 3的版本。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ffd6ffe",
   "metadata": {},
   "source": [
    "## 第一行Python代码\n",
    "\n",
    "安装完Python后，可以在命令行界面输入：\n",
    "\n",
    "```shell\n",
    "python\n",
    "```\n",
    "\n",
    "回车后进入一个Python解释器环境，得到类似如下的结果：\n",
    "\n",
    "```\n",
    "Python 3.9.10 (main, Jan 15 2022, 11:48:04)\n",
    "[Clang 13.0.0 (clang-1300.0.29.3)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>>\n",
    "```\n",
    "\n",
    "第一行Python代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11591c26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c5f76ff",
   "metadata": {},
   "source": [
    "可以将这句话写入一个文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "86c88a84",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing hello_world.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile hello_world.py\n",
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c1b1262",
   "metadata": {},
   "source": [
    "在命令行使用命令执行这个文件：\n",
    "\n",
    "```shell\n",
    "python hello_world.py\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f03789b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\r\n"
     ]
    }
   ],
   "source": [
    "!python hello_world.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f48d9196",
   "metadata": {},
   "source": [
    "## Python之禅\n",
    "\n",
    "可以通过`this`模块读取Python之禅的内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "48a37bd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Zen of Python, by Tim Peters\n",
      "\n",
      "Beautiful is better than ugly.\n",
      "Explicit is better than implicit.\n",
      "Simple is better than complex.\n",
      "Complex is better than complicated.\n",
      "Flat is better than nested.\n",
      "Sparse is better than dense.\n",
      "Readability counts.\n",
      "Special cases aren't special enough to break the rules.\n",
      "Although practicality beats purity.\n",
      "Errors should never pass silently.\n",
      "Unless explicitly silenced.\n",
      "In the face of ambiguity, refuse the temptation to guess.\n",
      "There should be one-- and preferably only one --obvious way to do it.\n",
      "Although that way may not be obvious at first unless you're Dutch.\n",
      "Now is better than never.\n",
      "Although never is often better than *right* now.\n",
      "If the implementation is hard to explain, it's a bad idea.\n",
      "If the implementation is easy to explain, it may be a good idea.\n",
      "Namespaces are one honking great idea -- let's do more of those!\n"
     ]
    }
   ],
   "source": [
    "import this"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ab06d5a",
   "metadata": {},
   "source": [
    "但这段代码其实是通过非常复杂的方式实现的，其源代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0f5acb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s = \"\"\"Gur Mra bs Clguba, ol Gvz Crgref\n",
      "\n",
      "Ornhgvshy vf orggre guna htyl.\n",
      "Rkcyvpvg vf orggre guna vzcyvpvg.\n",
      "Fvzcyr vf orggre guna pbzcyrk.\n",
      "Pbzcyrk vf orggre guna pbzcyvpngrq.\n",
      "Syng vf orggre guna arfgrq.\n",
      "Fcnefr vf orggre guna qrafr.\n",
      "Ernqnovyvgl pbhagf.\n",
      "Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.\n",
      "Nygubhtu cenpgvpnyvgl orngf chevgl.\n",
      "Reebef fubhyq arire cnff fvyragyl.\n",
      "Hayrff rkcyvpvgyl fvyraprq.\n",
      "Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.\n",
      "Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.\n",
      "Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.\n",
      "Abj vf orggre guna arire.\n",
      "Nygubhtu arire vf bsgra orggre guna *evtug* abj.\n",
      "Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.\n",
      "Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.\n",
      "Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!\"\"\"\n",
      "\n",
      "d = {}\n",
      "for c in (65, 97):\n",
      "    for i in range(26):\n",
      "        d[chr(i+c)] = chr((i+13) % 26 + c)\n",
      "\n",
      "print(\"\".join([d.get(c, c) for c in s]))\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(this.__file__) as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b7b418e",
   "metadata": {},
   "source": [
    "清理临时文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0de4044b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rm hello_world.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e08d3fc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
